/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.merchant;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.merchant.MerchantDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;

/**
 * 商户Service
 * @author Jackie.Liu
 * @version 2014-10-22
 */
@Component
@Transactional(readOnly = true)
public class MerchantService extends BaseService {

	@Autowired
	private MerchantDao merchantDao;
	
	public Merchant get(String id) {
		return merchantDao.get(id);
	}
	
	public Page<Merchant> find(Page<Merchant> page, Merchant merchant) {
		DetachedCriteria dc = merchantDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(merchant.getName())){
			dc.add(Restrictions.like("name", "%"+merchant.getName()+"%"));
		}
		dc.add(Restrictions.eq(Merchant.FIELD_DEL_FLAG, Merchant.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return merchantDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Merchant merchant) {
		merchantDao.save(merchant);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		merchantDao.deleteById(id);
	}
	
	public Merchant findByCreateBy(String createBy){
		Merchant merchant = merchantDao.findByCreateBy(createBy);
		if (merchant == null) {
			merchant = new Merchant();
		}
		return merchant;
	}
	
}
